﻿using Biz;
using BusinessLayer.Dal;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Models;
using Web.Models.ModelObjects;

namespace Web.Controllers
{
    public class HomeController : Controller
    {
        private DatabaseContext db = new DatabaseContext();
        Product prod = new Product();
        public HomeController()
            : base()
        {
        }

        public ActionResult Index()
        {
            /* ViewBag.CurrentUser = Models.User.CurrentUser;
            ViewBag.Roles = db.Roles.ToList();
            ViewBag.Users = db.Users.ToList();
            ViewBag.RoleAsManager = db.Roles.Where(r => r.Name.Contains("Quản lí")).First();
            ViewBag.AdminLogs = db.Logs.Where(l => l.User.Role.Id == 1).ToList(); */
            //ViewBag.FeaturedList = db.Products.Where(p => p.IsFeatured == true).ToList();
            ViewBag.Products = prod.GetAll().Take(6);
            ViewBag.FeaturedList = prod.GetAllFeatureProduct();
            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.ShowMainMenu = true;
            //ViewBag.CurrentUser = Models.User.CurrentUser;

            return View();
        }

        public ActionResult About()
        {
            ViewBag.ShowMainMenu = true;
            //ViewBag.CurrentUser = Models.User.CurrentUser;
            return View();
        }

        [HttpPost]
        public ActionResult UploadImage(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                var fileName = Path.GetFileName(file.FileName);
                // store the file inside ~/App_Data/uploads folder
                var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                file.SaveAs(path);
            }

            return View();
        }

        [NonAction]
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //User su = (User) Session[Models.User.USERNAME];

            //if (su != null)
            //{
            //    User u = db.Users.Where(qu => qu.Username == su.Username && qu.Password == su.Password).First();
            //    if (u != null)
            //    {
            //        Models.User.CurrentUser = u;
            //    }
            //}

            //base.OnActionExecuting(filterContext);
        }
    }
}
